package com.ss.android.article.base.feature.subscribe.b;

import android.content.Context;
import android.os.Handler;

import com.bytedance.article.common.c.TTUtils;
import com.ss.android.account.Spipe;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.util.NetworkUtils;
import java.util.List;
import org.json.JSONObject;

final class a extends AbsApiThread {
    private final Context a;
    private final Handler b;
    private i c;
    private final boolean d;

    public a(Context arg7, Handler arg8) {
        super("EntryGroupListThread");
        this.a = arg7.getApplicationContext();
        this.b = arg8;
        this.c = new i();
        AppData v0 = AppData.getInstance();
        boolean v0_1 = System.currentTimeMillis() - v0.mLastSyncEntryGroupListTime > (((long)v0.Q)) * 1000 ? true : false;
        this.d = v0_1;
    }

    private static boolean a(Context arg2, i arg3) {
        boolean v0_2;
        try {
            DBHelper v0_1 = DBHelper.getInstance(arg2);
            Spipe.inst();
            arg3.c = v0_1.queryEntryGroupList();
            v0_2 = true;
        }
        catch(Exception v0) {
            v0_2 = false;
        }

        return v0_2;
    }

    private static boolean b(Context arg3, i arg4) {
        boolean v0 = false;
        int v1 = 204800;
        try {
            String v1_2 = NetworkUtils.doGet(v1, ArticleConstants.entryListUrlV1);
            if(v1_2 == null) {
                return v0;
            }

            if(v1_2.length() == 0) {
                return v0;
            }

            JSONObject v2 = new JSONObject(v1_2);
            if(!AbsApiThread.isApiSuccess(v2)) {
                return v0;
            }

            arg4.c = android.support.design.a.b(v2.optJSONArray("data"));
            v0 = true;
        }
        catch(Exception v1_1) {
            TTUtils.getErrorCode(arg3, ((Throwable)v1_1));
        }

        return v0;
    }

    public final void run() {
        int v3;
        int v0;
        int v1 = 0;
        if(this.c.a) {
            v0 = a(this.a, this.c) ? 0 : 1;
            if(this.c.c == null || this.c.c.size() <= 0) {
                v0 = 0;
                v3 = 0;
            }else {
                this.b.obtainMessage(1, v0, 1, this.c).sendToTarget();
                v0 = 1;
                v3 = 1;
            }
        } else {
            v0 = 0;
            v3 = 0;
        }

        if(!this.d && v0 != 0) {
            this.c.b = false;
        }

        if(this.c.b) {
            if(!b(this.a, this.c)) {
                v1 = 1;
            }
            else if(this.c.c != null && this.c.c.size() > 0) {
                DBHelper v0_1 = DBHelper.getInstance(this.a);
                List v3_1 = this.c.c;
                Spipe.inst();
                v0_1.insertEntryGroupList(v3_1);
            }

            this.b.obtainMessage(1, v1, 2, this.c).sendToTarget();
            v3 = 1;
        }

        if(v3 == 0) {
            this.b.obtainMessage(1, 1, 1, this.c).sendToTarget();
        }
    }
}

